
#include "msg_mater.h"
#include "msg_err.h"
#include "page_base.h"
#include "img_common.h"
#include "sys_scene.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "pt_dev.h"
#include "sys_font.h"
#include "pt_fs.h"
#include "msg_telephone.h"


static ui_msg_mater_t  ui_msg_mater;


static char* icon_msg_mater[1] = 
{
    "Warning.png",
};


static int msg_mater_cancel_clicked_event(void)
{
    debug_d("mater cancel\n");

    pop_win_unload();

    return 0;
}

static void msg_mater_cancel_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) 
    {
        msg_mater_cancel_clicked_event();
    }
}

static int msg_mater_yes_clicked_event(void)
{
    debug_d("msg mater yes\n");

    pop_win_unload();
    pop_win_load("msg_tele",lv_page_msg_telephone_create(),NULL);

    return 0;
}

static void msg_mater_yes_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED)
    {
        msg_mater_yes_clicked_event();
    }
}


int ui_msg_mater_create(page_base_t* self)
{
    char* txt_buff = NULL;

    ui_msg_mater_t* u_mater   = NULL;
    lv_obj_t      * superview = NULL;
    
    superview = self->root;
    u_mater   = (ui_msg_mater_t*)self->user_data;
    //**********************************************************************************************
    u_mater->ui.img_tip = lv_img_create(superview);
    lv_img_set_src(u_mater->ui.img_tip,pt_fs_get(icon_msg_mater[0]));
    lv_obj_set_pos(u_mater->ui.img_tip,200,22);
    //**********************************************************************************************
    u_mater->ui.lab_tip = lv_label_create(superview);
    lv_obj_set_pos(u_mater->ui.lab_tip,24,124);
    lv_obj_set_size(u_mater->ui.lab_tip,432,72);
    lv_obj_set_style_text_align(u_mater->ui.lab_tip,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(u_mater->ui.lab_tip,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(u_mater->ui.lab_tip,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(u_mater->ui.lab_tip, lv_font_get(20), LV_STATE_DEFAULT);

    txt_buff = psram_malloc(1024);

    memset(txt_buff,0,1024);
    snprintf(txt_buff,1024,"[%s][%s]余量不足,请及时补充或更换\n(可前往美的美居APP查看详情.)",u_mater->data.dev_name,u_mater->data.dev_consum_name);
    lv_label_set_text(u_mater->ui.lab_tip,txt_buff);
    //**********************************************************************************************
    //cancel btn

    u_mater->ui.bg_cancel = lv_obj_create(superview);

    lv_obj_set_pos(u_mater->ui.bg_cancel,34,246);
    lv_obj_set_size(u_mater->ui.bg_cancel,170,50);

	lv_obj_set_style_radius(u_mater->ui.bg_cancel, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(u_mater->ui.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(u_mater->ui.bg_cancel, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(u_mater->ui.bg_cancel, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(u_mater->ui.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(u_mater->ui.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(u_mater->ui.bg_cancel, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(u_mater->ui.bg_cancel, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(u_mater->ui.bg_cancel,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(u_mater->ui.bg_cancel,LV_OPA_100,0);
    lv_obj_set_style_bg_color(u_mater->ui.bg_cancel,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(u_mater->ui.bg_cancel, msg_mater_cancel_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    u_mater->ui.lab_cancel = lv_label_create(u_mater->ui.bg_cancel);
    lv_obj_set_pos(u_mater->ui.lab_cancel,0,11);
    lv_obj_set_size(u_mater->ui.lab_cancel,170,30);
    lv_obj_set_style_text_align(u_mater->ui.lab_cancel,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(u_mater->ui.lab_cancel,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(u_mater->ui.lab_cancel,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(u_mater->ui.lab_cancel, lv_font_get(28), LV_STATE_DEFAULT);

    lv_label_set_text(u_mater->ui.lab_cancel,"知道了");
    //**********************************************************************************************
    //**********************************************************************************************
    //yes btn

    u_mater->ui.bg_yes = lv_obj_create(superview);

    lv_obj_set_pos(u_mater->ui.bg_yes,249,246);
    lv_obj_set_size(u_mater->ui.bg_yes,170,50);

	lv_obj_set_style_radius(u_mater->ui.bg_yes, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(u_mater->ui.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(u_mater->ui.bg_yes, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(u_mater->ui.bg_yes, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(u_mater->ui.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(u_mater->ui.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(u_mater->ui.bg_yes, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(u_mater->ui.bg_yes, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(u_mater->ui.bg_yes,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(u_mater->ui.bg_yes,LV_OPA_100,0);
    lv_obj_set_style_bg_color(u_mater->ui.bg_yes,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(u_mater->ui.bg_yes, msg_mater_yes_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    u_mater->ui.lab_yes = lv_label_create(u_mater->ui.bg_yes);
    lv_obj_set_pos(u_mater->ui.lab_yes,0,11);
    lv_obj_set_size(u_mater->ui.lab_yes,170,30);
    lv_obj_set_style_text_align(u_mater->ui.lab_yes,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(u_mater->ui.lab_yes,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(u_mater->ui.lab_yes,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(u_mater->ui.lab_yes, lv_font_get(28), LV_STATE_DEFAULT);

    memset(txt_buff,0,1024);
    sprintf(txt_buff,"联系售后");

    lv_label_set_text(u_mater->ui.lab_yes,txt_buff);
    //**********************************************************************************************
    psram_free(txt_buff);
    txt_buff = NULL;


    return 0;

}

int ui_msg_mater_destroy(page_base_t* self)
{
    ui_msg_mater_t* u_mater   = NULL;
    lv_obj_t      * superview = NULL;
    
    superview = self->root;
    u_mater   = (ui_msg_mater_t*)self->user_data;

    if(!u_mater){
        return -1;
    }

    if(u_mater->ui.img_tip){
        lv_obj_del(u_mater->ui.img_tip);
        u_mater->ui.img_tip = NULL;
    }

    if(u_mater->ui.lab_tip){
        lv_obj_del(u_mater->ui.lab_tip);
        u_mater->ui.lab_tip = NULL;
    }    

    if(u_mater->ui.bg_cancel){
        lv_obj_del(u_mater->ui.bg_cancel);
        u_mater->ui.bg_cancel = NULL;
        u_mater->ui.lab_cancel = NULL;
    }    
    if(u_mater->ui.bg_yes){
        lv_obj_del(u_mater->ui.bg_yes);
        u_mater->ui.bg_yes = NULL;
        u_mater->ui.lab_yes = NULL;
    }   

    return 0;
}


static void msg_mater_on_custom_attr_config(page_base_t* self)
{


}
static void msg_mater_on_view_load(page_base_t* self)
{


}
static void msg_mater_on_view_did_load(page_base_t* self)
{
    ui_msg_mater_create(self);
}
static void msg_mater_on_view_will_appear(page_base_t* self)
{


}
static void msg_mater_on_view_did_appear(page_base_t* self)
{


}
static void msg_mater_on_view_will_disappear(page_base_t* self)
{


}
static void msg_mater_on_view_did_disappear(page_base_t* self)
{


}
static void msg_mater_on_view_did_unload(page_base_t* self)
{


}


static page_vtable_t page_msg_mater_param = {

    .on_custom_attr_config  = msg_mater_on_custom_attr_config,
    .on_view_load           = msg_mater_on_view_load,
    .on_view_did_load       = msg_mater_on_view_did_load,
    .on_view_will_appear    = msg_mater_on_view_will_appear,
    .on_view_did_appear     = msg_mater_on_view_did_appear,
    .on_view_will_disappear = msg_mater_on_view_will_disappear,
    .on_view_did_disappear  = msg_mater_on_view_did_disappear,
    .on_view_did_unload     = msg_mater_on_view_did_unload,
};


page_vtable_t* lv_page_msg_mater_create(void)
{
    return &page_msg_mater_param;
}

dev_info_t* msg_mater_reg(ui_mater_data_t* data)
{
    int len = 0;
    ui_msg_mater_t* ui_mater = NULL;
    page_vtable_t*  method   = NULL;
    dev_info_t*     devinfo  = NULL;

    devinfo = (dev_info_t*)malloc(sizeof(dev_info_t));
    if(!devinfo){
        debug_d("merr\n");
        return NULL;
    }

    ui_mater = (ui_msg_mater_t*)malloc(sizeof(ui_msg_mater_t));
    if(!ui_mater){
        debug_d("merr\n");
        return NULL;
    }

    method = (page_vtable_t*)malloc(sizeof(page_vtable_t));
    if(!method){
        debug_d("merr\n");
        return NULL;
    }
    //***********************************************************
    method->on_custom_attr_config  = msg_mater_on_custom_attr_config;
    method->on_view_load           = msg_mater_on_view_load;
    method->on_view_did_load       = msg_mater_on_view_did_load;
    method->on_view_will_appear    = msg_mater_on_view_will_appear;
    method->on_view_did_appear     = msg_mater_on_view_did_appear;
    method->on_view_will_disappear = msg_mater_on_view_will_disappear;
    method->on_view_did_disappear  = msg_mater_on_view_did_disappear;
    method->on_view_did_unload     = msg_mater_on_view_did_unload;
    //***********************************************************
    memset(&ui_mater->ui,0,sizeof(ui_mater_obj_t));
    memcpy(&ui_mater->data,data,sizeof(ui_mater_data_t));
    //***********************************************************
    devinfo->method     = method;    
    devinfo->priv_data  = (void*)ui_mater;
    //***********************************************************

    return devinfo;
}

int msg_mater_unreg(dev_info_t* devinfo)
{
    


    return 0;
}